package org.glut.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.glut.backend.domain.LoginResult;
import org.glut.backend.service.IUserService;
import org.glut.backend.domain.User;
import org.glut.backend.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * 用户表 服务实现类
 *
 * @author gluglu2
 * @since 2025-08-02 14:30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    /**
     * @description: 用户密码登录
     * @param phone
     * @param password
     * @return org.glut.backend.domain.User
     * @author: gluglu2
     * @date: 2025/8/4 10:41
     */
    @Override
    public LoginResult passwordLogin(String phone, String password) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, phone);
        User user = this.getOne(queryWrapper);

        LoginResult result = new LoginResult();

        if (user == null) {
            result.setCode(1);
            result.setMsg("用户不存在");
            return result;
        }

        if (!password.equals(user.getPassword())) {
            result.setCode(2);
            result.setMsg("密码错误");
            return result;
        }

        result.setCode(0);
        result.setMsg("登录成功");
        result.setUser(user);
        return result;
    }
}
